/**
 * @file sleep_info.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SLEEP!= 0

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/


/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/
#define SLEEP_INFO_EDGE_SPACE  (LV_HOR_RES / 12)

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * sleep_info_create(lv_obj_t * tileview, uint16_t id)
{
    lv_sleep_obj_ext_t * ext = sleep_get_ext();
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;

    lv_obj_t * tab = sleep_tileview_add_tile(tileview, id);
    LV_ASSERT_MEM(tab);
    if(tab == NULL) return NULL;

    lv_obj_t * content = lv_cont_create(tab, NULL);
    lv_obj_add_style(content, LV_CONT_PART_MAIN, &lv_style_transp_tight);
    lv_cont_set_fit(content, LV_FIT_TIGHT);
    lv_cont_set_layout(content, LV_LAYOUT_OFF);
    lv_obj_set_click(content, false);

    lv_bidi_dir_t bidi_dir = lv_obj_get_base_dir(content);

    lv_obj_t * title = lv_label_create(content, NULL);
    char strall[50];
    if(LV_BIDI_DIR_RTL == bidi_dir) {
        lv_obj_set_base_dir(title, LV_BIDI_DIR_LTR);
        snprintf(strall, 50, "%02d:%02d-%02d:%02d %s",
                 ext->time->longsleep_end_hour, ext->time->longsleep_end_min,
                 ext->time->longsleep_start_hour, ext->time->longsleep_start_min,
                 (char *)lv_lang_get_text(WATCH_TEXT_ID_SLEEP_NIGHT));
    } else {
        snprintf(strall, 50, "%s %02d:%02d-%02d:%02d",
                 (char *)lv_lang_get_text(WATCH_TEXT_ID_SLEEP_NIGHT),
                 ext->time->longsleep_start_hour, ext->time->longsleep_start_min,
                 ext->time->longsleep_end_hour, ext->time->longsleep_end_min);
    }
    lv_obj_add_style(title, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_text(title, strall);

    lv_coord_t max_w = lv_obj_get_width(tab) - 2 * SLEEP_INFO_EDGE_SPACE;
    if(max_w < lv_obj_get_width(content)) {
        lv_cont_set_fit2(content, LV_FIT_NONE, LV_FIT_TIGHT);
        lv_obj_set_width(content, max_w);
        lv_label_set_long_mode(title, LV_LABEL_LONG_SROLL);
        lv_obj_set_width(title, max_w);
        lv_obj_align(title, content, LV_ALIGN_IN_TOP_MID, 0, 0);
    }

    lv_coord_t title_h = lv_obj_get_height(title);
    lv_coord_t title_w = lv_obj_get_width(title);

    /* long sleep */
    lv_obj_t * img_sleep = lv_img_create(content, NULL);
    lv_img_set_src(img_sleep, ICON_SLEEP_IMAGE);

    int32_t hour = ext->time->longsleep_sum_min / 60;
    int32_t min = ext->time->longsleep_sum_min % 60;
    if(bidi_dir == LV_BIDI_DIR_RTL)
        snprintf(strall, 50, "%s%02d%s%02d", (char *)lv_lang_get_text(WATCH_TEXT_ID_MINUTES), min,
                 (char *)lv_lang_get_text(WATCH_TEXT_ID_HOURS), hour);
    else
        snprintf(strall, 50, "%02d%s%02d%s", hour, (char *)lv_lang_get_text(WATCH_TEXT_ID_HOURS),
                 min, (char *)lv_lang_get_text(WATCH_TEXT_ID_MINUTES));
    lv_obj_t * label_longsleep = lv_label_create(content, NULL);
    lv_obj_add_style(label_longsleep, LV_LABEL_PART_MAIN, &lv_watch_font30);
    lv_label_set_text(label_longsleep, strall);
    lv_coord_t longsleep_h = lv_obj_get_height(label_longsleep);

    lv_coord_t inner = LV_HOR_RES / 20;
    lv_coord_t max_inner = (lv_obj_get_height(tab) - 2 * title_h - 2 * longsleep_h -
                            2 * SLEEP_INFO_EDGE_SPACE) / 3;
    if(max_inner < inner) {
        inner = max_inner;
    }

    lv_coord_t img_w = lv_obj_get_width(img_sleep);
    lv_coord_t img_h = lv_obj_get_height(img_sleep);
    lv_img_set_zoom(img_sleep, LV_IMG_ZOOM_NONE * longsleep_h / img_h);
    lv_coord_t img_real_w = longsleep_h * img_w / img_h;
    lv_coord_t img_x = (img_real_w - img_w) / 2;
    lv_coord_t title_img_space = inner + (longsleep_h - img_h) / 2;
    lv_coord_t img_label_space = 2;
    if(LV_BIDI_DIR_RTL == bidi_dir) {
        lv_obj_align(img_sleep, title, LV_ALIGN_OUT_BOTTOM_RIGHT, -img_x, title_img_space);
        lv_obj_align(label_longsleep, title, LV_ALIGN_OUT_BOTTOM_RIGHT,
                     -img_real_w - img_label_space, inner);
    } else {
        lv_obj_align(img_sleep, title, LV_ALIGN_OUT_BOTTOM_LEFT, img_x, title_img_space);
        lv_obj_align(label_longsleep, title, LV_ALIGN_OUT_BOTTOM_LEFT,
                     img_real_w + img_label_space, inner);
    }

    title = lv_label_create(content, title);//NULL);
    lv_obj_add_style(title, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    if(LV_BIDI_DIR_RTL == bidi_dir) {
        lv_label_set_align(title, LV_LABEL_ALIGN_RIGHT);
    }
    lv_label_set_long_mode(title, LV_LABEL_LONG_SROLL);
    lv_obj_set_width(title, title_w);
    lv_label_set_text_id(title, WATCH_TEXT_ID_SPORADIC_NAPS);
    lv_obj_align(title, content, LV_ALIGN_IN_TOP_MID, 0,
                 lv_obj_get_height(title) + longsleep_h + 2 * inner);

    /* Sporadic naps */
    img_sleep = lv_img_create(content, NULL);
    lv_img_set_src(img_sleep, ICON_SLEEP_IMAGE);
    lv_img_set_zoom(img_sleep, LV_IMG_ZOOM_NONE * longsleep_h / img_h);

    hour = ext->time->shortsleep_sum_min / 60;
    min = ext->time->shortsleep_sum_min % 60;
    if(bidi_dir == LV_BIDI_DIR_RTL)
        snprintf(strall, 50, "%s%02d%s%02d", (char *)lv_lang_get_text(WATCH_TEXT_ID_MINUTES), min,
                 (char *)lv_lang_get_text(WATCH_TEXT_ID_HOURS), hour);
    else
        snprintf(strall, 50, "%02d%s%02d%s", hour, (char *)lv_lang_get_text(WATCH_TEXT_ID_HOURS),
                 min, (char *)lv_lang_get_text(WATCH_TEXT_ID_MINUTES));
    lv_obj_t * label_sporadicnaps = lv_label_create(content, label_longsleep);
    lv_label_set_text(label_sporadicnaps, strall);

    if(LV_BIDI_DIR_RTL == bidi_dir) {
        lv_obj_align(img_sleep, title, LV_ALIGN_OUT_BOTTOM_RIGHT, -img_x, title_img_space);
        lv_obj_align(label_longsleep, title, LV_ALIGN_OUT_BOTTOM_RIGHT,
                     -img_real_w - img_label_space, inner);
    } else {
        lv_obj_align(img_sleep, title, LV_ALIGN_OUT_BOTTOM_LEFT, img_x, title_img_space);
        lv_obj_align(label_longsleep, title, LV_ALIGN_OUT_BOTTOM_LEFT,
                     img_real_w + img_label_space, inner);
    }

    lv_obj_align(content, tab, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * label_content = lv_cont_create(tab, NULL);
    lv_obj_add_style(label_content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_size(label_content, 30,30);
    lv_cont_set_layout(label_content, LV_LAYOUT_OFF);
    lv_obj_set_click(label_content, false);
    lv_obj_t * label_up = lv_label_create(label_content, NULL);
    lv_label_set_text(label_up, LV_SYMBOL_DOWN);
    lv_obj_align(label_up, label_content, LV_ALIGN_CENTER, 0, 0);
    lv_obj_align(label_content, tab, LV_ALIGN_IN_BOTTOM_MID, 0, -5);

    return tab;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/


#endif /*USE_LV_WATCH_SLEEP*/
